package lims.client;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.print.attribute.Attribute;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;



@SuppressWarnings({ "serial"})
public class DeviceTableModel extends Vector<LimsPrinter> implements TableModel,Serializable{
	private static DeviceTableModel instance=new DeviceTableModel();
	public static DeviceTableModel getInstance(){
		return instance;
	}
	
	
			
	List<String> columns=new ArrayList<String>();
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeObject("start DeviceTableModel");
		out.writeInt(size());
		for(LimsPrinter tmp:this){
			out.writeObject(tmp);
		}
		out.writeObject("finish DeviceTableModel");
	}
	
	private void readObject(java.io.ObjectInputStream in)  throws IOException, ClassNotFoundException{
		if(!in.readObject().equals("start DeviceTableModel")) throw new IOException("flag1 error");
		Integer size=in.readInt();
		List<LimsPrinter> nl=new ArrayList<LimsPrinter>();
		for(int i=0;i<size;i++){
			LimsPrinter tmp=(LimsPrinter) in.readObject();
			nl.add(tmp);
		}			
		if(!in.readObject().equals("finish DeviceTableModel")) throw new IOException("flag2 error");
		clear();
		addAll(nl);
	}
	
		 
	public List<TableModelListener> listeners=new ArrayList<TableModelListener>();
	protected DeviceTableModel(){
		if(instance==null) instance=this;
		columns.add("名称");
		columns.add("打印机");
		columns.add("纸张");
		columns.add("说明");
	}
	
	@Override
	public int getRowCount() {
		return super.size();
	}

	@Override
	public int getColumnCount() {
		return columns.size();
	}

	@Override
	public String getColumnName(int columnIndex) {
		return columns.get(columnIndex);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return Object.class;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
			LimsPrinter limsPrinter=super.get(rowIndex);
			if(limsPrinter==null) return null;
			switch(columnIndex){
			case 0:
				return limsPrinter.name;
			case 1:
				return limsPrinter.serviceName;
			case 2:
				String ret="";
				for(Attribute attr:limsPrinter.attributes.toArray()){
					//
					/*if(attr instanceof MediaSizeName){
						MediaSize size=
						MediaSize.getMediaSizeForName((MediaSizeName) attr);
						ret+=size.getMediaSizeName()+" ";
					}else*/ 
					ret+=attr.toString()+" ";
					/*
					if(attr instanceof OrientationRequested){
						OrientationRequested orientation=(OrientationRequested) attr;
						ret+=orientation.toString()+" ";
					}	
					
					if(attr instanceof MediaPrintableArea){
						MediaPrintableArea printableArea=(MediaPrintableArea) attr;
						ret+=printableArea.toString()+" ";
					}	*/
				}
				return ret;
			case 3:
				return limsPrinter.description;
			}
			return null;
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
	}

	@Override
	public void addTableModelListener(TableModelListener l) {
		if(listeners.contains(l)) return;
		listeners.add(l);
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		if(!listeners.contains(l)) return;
		listeners.remove(l);
	}
	
	public void afterChange(){
		for(TableModelListener l:listeners){
			l.tableChanged(new TableModelEvent(this));
		}
	}

	public LimsPrinter getPrinter(String printerName) {
		for(LimsPrinter tmp:this){
			if(tmp.name.equals(printerName)) return tmp;
		}
		return null;
	}

	/*public String loadConfig(String path) {
		try{
			FileInputStream fos=new FileInputStream(new File(path));	
			ObjectInputStream oos=new ObjectInputStream(fos);
			clear();
			DeviceTableModel tmp=(DeviceTableModel) oos.readObject();
			addAll(tmp);
			oos.close();
			afterChange();
			return "加载成功";
		}catch(Exception e){
			return e.getMessage();
		}
	}*/
	
}